import com.google.common.base.Strings;
import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.CsvFileSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;


import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

import static java.lang.Thread.sleep;

/**
 * @Author：徐志荣
 * @Date：2023/9/26 下午 3:54
 */
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class BlogCases extends InitAndEnd{

    public static Stream<Arguments> Generator() {
        return Stream.of(
                Arguments.arguments("http://8.130.87.189:8080/blog_content.html?aid=",
                        "博客正文",
                        "redis的常用场景"));
    }

    /**
    * 测试登录功能
    *
    * @return
    */
    @Order(1)
    @ParameterizedTest
    @CsvFileSource(resources = "LoginSuccess.csv")
     void LoginSucess(String username,String password,String blog_list_url) throws InterruptedException {


        webDriver.get("http://8.130.87.189:8080/login.html");
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);

        webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);

        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);

        webDriver.findElement(By.cssSelector("#submit")).click();
        webDriver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);

        sleep(3000);
        //跳转到列表页
        //获取到当前页面url
        String cur_url = webDriver.getCurrentUrl();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        Assertions.assertEquals(blog_list_url,cur_url);
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);

        String cur_user = webDriver.findElement(By.cssSelector("#username")).getText();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        Assertions.assertEquals(username,cur_user);

    }

    /**
    *
    *  测试博客列表博客数量不为0
    * @return
    */
    @Order(3)
    @Test
    void BlogList(){
        //打开博客列表页
        webDriver.get("http://8.130.87.189:8080/myblog_list.html");
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);

        //获取页面上所有博客标题 对应的元素
        int title_num = webDriver.findElements(By.cssSelector("#article-list-div > div > div > a > div.title")).size();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);

        //如果元素数量不为0，测试通过
        Assertions.assertNotEquals(0,title_num);


    }

    /**
    * 博客详情页
    *
    * @return
    */
    @Order(4)
    @ParameterizedTest
    @MethodSource("Generator")
    void BlogDetail(String expected_url,String expected_title,String expected_blog_title ){
        //找到第一篇博客对应的 点击链接
        webDriver.findElement(By.xpath("//*[@id=\"article-list-div\"]/div/div/a/div[1]/h4")).click();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);

        //切换窗口
        String currentWindowHandle = webDriver.getWindowHandle();
        Set<String> windowHandles = webDriver.getWindowHandles();
        for (String windowHandle : windowHandles) {
            if (!windowHandle.equals(currentWindowHandle)) {
                webDriver.switchTo().window(windowHandle);
                break;
            }
        }

        //获取当前页面的url
        String cur_url = webDriver.getCurrentUrl();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);

        //获取当前页面的 title
        String cur_title = webDriver.getTitle();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        //获取博客标题
        String blog_title = webDriver.findElement(By.cssSelector("#title")).getText();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
       // Assertions.assertEquals(expected_url,cur_url);
        if(cur_url.contains(expected_url)){
            System.out.println("测试通过");
        }else{
            System.out.println("测试不通过");

        }
        Assertions.assertEquals(expected_title,cur_title);
        Assertions.assertEquals(expected_blog_title,blog_title);


    }

    /**
     * 写博客
     *
     *
     * @return
     */
    @Order(2)
    @Test
    void EditBlog() throws InterruptedException {
        //找到写博客按钮，点击
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).click();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);

        //通过js将标题输入
        ((JavascriptExecutor)webDriver).executeScript("document.getElementById(\"title\").value=\"redis的常用场景\"");
       sleep(3000);
        //点击发布
        webDriver.findElement(By.cssSelector("#bt")).click();

        //上传图片
        WebElement file = webDriver.findElement(By.cssSelector("#file"));
        file.sendKeys("D:\\work\\boke\\11_9_类和对象.assets\\测试.png");
        sleep(3000);

        //输入文章摘要
        webDriver.findElement(By.cssSelector("#summary")).sendKeys("redis通常用于，缓存。");

        //发布
        webDriver.findElement(By.cssSelector("#submit")).click();
        sleep(3000);

        //判断是否继续添加文章？ 不继续添加文章。
        webDriver.switchTo().alert().dismiss();
        sleep(3000);
        //获取当前页面url
        String cur_url = webDriver.getCurrentUrl();
        sleep(3000);
        //Assertions.assertEquals("http://8.130.87.189:8080/myblog_list.html",cur_url);

    }
    /**
    * 校验已经发布博客标题
    * 校验已经发布博客时间
    * @return
    */
    @Order(5)
    @Test
    void  BlogInfoChecked(){
        webDriver.get("http://8.130.87.189:8080/myblog_list.html");
        //获取第一篇博客标题
       String first_blog_title =  webDriver.findElement(By.cssSelector("#article-list-div > div:nth-child(1) > div > a > div.title > h4")).getText();
        //获取第一篇博客发布时间
       String first_blog_time =  webDriver.findElement(By.cssSelector("#article-list-div > div:nth-child(1) > div > div > a:nth-child(1) > div")).getText();
        //校验博客标题是不是 redis的常用场景
        Assertions.assertEquals("redis的常用场景",first_blog_title);
        if(first_blog_time.contains("2023")){
            System.out.println("测试通过");
        }else{
            System.out.println("测试不通过");
        }


    }

    /**
    * 检验删除
    *
    * @return
    */
    @Order(6)
    @Test
    void DeleteBlog() throws InterruptedException {
        //打开博客页面
        webDriver.get("http://8.130.87.189:8080/myblog_list.html");
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        //删除按钮
        webDriver.findElement(By.cssSelector("#article-list-div > div:nth-child(1) > div > div > a.delete")).click();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        sleep(3000);
        webDriver.switchTo().alert().accept();
        sleep(3000);

        webDriver.switchTo().alert().accept();
        //博客页面第一篇页面不是 “redis...”
        String first_blog_title = webDriver.findElement(By.cssSelector("#article-list-div > div > div > a > div.title > h4")).getText();
        Assertions.assertNotEquals(first_blog_title,"redis的常用场景");

    }

    /**
    *  注销功能
    *
    * @return
    */
    @Order(7)
    @Test
    void LogOut(){
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(7)")).click();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        webDriver.switchTo().alert().accept();

        //校验url
        String currentUrl = webDriver.getCurrentUrl();
        Assertions.assertEquals("http://8.130.87.189:8080/login.html",currentUrl);
        //校验登录按钮
        WebElement webElement = webDriver.findElement(By.cssSelector("#submit"));
        Assertions.assertNotNull(webElement);

    }



}
